package com.jlect.swebing.ui.client.button;

import com.jlect.swebing.ui.client.ActionListener;
import com.jlect.swebing.ui.client.ActionListenerCollection;
import com.jlect.swebing.ui.client.MouseEvent;
import com.jlect.swebing.ui.client.MouseListenerAdapter;

/**
 * Implements default button behaviour. It doesn't change state of the control and simply redirect events
 * to the component listeners
 *
 * @author Sergey Kozmin
 * @since 22.11.2007 23:12:18
 */
public class DefaultButtonController extends MouseListenerAdapter implements ButtonController {
    private ButtonView view;
    private ActionListenerCollection clickListeners;

    public void setModel(ButtonModel model) {
    }

    public void setView(ButtonView view) {
        if(view == null) {
            throw new IllegalArgumentException("View object could not be null. ");
        }
        this.view = view;
    }

    public void addActionListener(ActionListener listener) {
        getClickListener().add(listener);
    }

    public void removeActionListener(ActionListener listener) {
        getClickListener().remove(listener);
    }

    private ActionListenerCollection getClickListener() {
        if (clickListeners == null) {
            clickListeners = new ActionListenerCollection();
        }
        return clickListeners;
    }

    public void mouseClicked(MouseEvent e) {
        getClickListener().clickActionPerformed(e);
    }
}
